package com.jc.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jc.order.common.DictionaryToJcConst;
import com.jc.order.constant.DictConst;
import com.jc.order.constant.OrderConst;
import com.jc.order.constant.ValidateErrorConst;
import com.jc.order.dto.JcPolicyDTO;
import com.jc.order.dto.SystemDictDataDTO;
import com.jc.order.service.SystemDictDataService;
import com.jc.util.Result;
import com.jc.vo.PolicyRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 跨境保险校验策略
 */
@Slf4j
@Service("insuranceTraditionalValidateService")
public class InsuranceTraditionalValidateServiceImpl extends CargoValidateServiceImpl {

    @Autowired
    private SystemDictDataService systemDictDataService;


    @Override
    public Result validateRequest(PolicyRequest request, JcPolicyDTO dto) {
        Result r = new Result<>();
        List<String> errorList = new ArrayList<>();
        if (request != null) {
            validateCommonFields(request, dto, errorList);
        }
        if (errorList.size() == 0) {
            r.ok(dto);
        } else {
            r.error().setContent(errorList);
        }
        return r;
    }

    @Override
    public Result validateDTO(JcPolicyDTO dto) {
        Result result = new Result<>();
        List<String> errorList = new ArrayList<>();
        this.cargoValueValid(errorList, dto);
        this.currencyValid(errorList, dto);
        super.freightValid(errorList, dto);
        super.freightCurrencyValid(errorList, dto);


        if (!OrderConst.PREMIUM_CALCULATION.equalsIgnoreCase(dto.getLogic())) {
            super.getAttachment(dto);
            super.startTimeValid(errorList, dto);
            super.fromAddressValid(errorList, dto);
            super.toAddresssValid(errorList, dto);
            super.fromCountryValid(errorList, dto);
            super.transferCountryValid(errorList, dto);
            super.transferAddressValid(errorList, dto);
            super.toCountryValid(errorList, dto);
            super.insuredValid(errorList, dto);
            super.transportModeValid(errorList, dto);
            super.validateBaseAmountWay(errorList, dto);

            super.trackingNoLengthValid(errorList, dto);
            super.chargeableWeight(errorList, dto);
            super.blnameValid(errorList, dto);
            super.waybillNumberValid(errorList, dto);
            super.packingValid(errorList, dto);
            super.packingQuantityValid(errorList, dto);
            super.cargoDescValid(errorList, dto);
            this.cargoCategoryValid(errorList, dto);
            this.startTimeDisplayModeValid(errorList, dto);

        }

        if (errorList.size() == 0) {
            result.ok(dto);
        } else {
            result.error().setContent(errorList);
        }
        return result;
    }


    private void validateCommonFields(PolicyRequest request, JcPolicyDTO p, List<String> errorList) {
        //运输方式校验
        super.validateTransportType(request, p, errorList);
        //货物类型校验
        this.validateGoodsType(request, p, errorList);
        //包装类型校验
        super.validateGoodsPackage(request, p, errorList);
    }

    // 货物类型校验
    private void validateGoodsType(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getCargoCategoryCode())) {
            Object value = processPackingCode(request.getCargoCategoryCode(), DictionaryToJcConst.T_GOODSTYPE_MAP, errorList, "货物类别");
            if (value != null) {
                dto.setGoodsType(Integer.valueOf(value.toString()));
            }
        }
    }

    /**
     * 起运日期显示方式
     *
     * @param errorList
     * @param p
     */
    private void startTimeDisplayModeValid(List<String> errorList, JcPolicyDTO p) {
        Byte startTimeDisplayMode = p.getStartTimeDisplayMode();
        if (startTimeDisplayMode == null) {
            errorList.add("起运日期显示方式不能为空");
        } else {
            SystemDictDataDTO dictionaryDTO = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_START_TIME_DISPLAY_MODE, startTimeDisplayMode.toString());
            if (dictionaryDTO == null) {
                errorList.add("起运日期显示方式匹配错误;");
            }
        }
    }





    /**
     * 货物类别
     *
     * @param errorList
     * @param p
     */
    private void cargoCategoryValid(List<String> errorList, JcPolicyDTO p) {
        Integer goodsType = p.getGoodsType();
        if (goodsType == null) {
            errorList.add(ValidateErrorConst.ERROR_CARGO_CATEGORY_EMPTY);
        } else {
            SystemDictDataDTO dictionaryDTO = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_TRANDITIONAL_GOODS_TYPE, goodsType.toString());
            if (dictionaryDTO == null) {
                errorList.add(ValidateErrorConst.ERROR_CARGO_CATEGORY_MISMATCH);
            }
        }
    }


}
